/**
 * 
 */
package com.fita.admin.visitor;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.utils.EnumUtils;
import com.demo2do.core.visitor.AbstractObjectRoot;
import com.fita.admin.visitor.impl.CloudContextVisitor;
import com.fita.admin.visitor.impl.ConfigVisitor;
import com.fita.admin.visitor.impl.PersistentVisitor;
import com.fita.admin.visitor.impl.SecureVisitor;
import org.springframework.cache.annotation.Cacheable;

import java.lang.reflect.Method;
import java.util.Map;



/**
 * @author Wilson
 * 
 */
public class ApplicationObjectRoot extends AbstractObjectRoot {

    private PersistentVisitor persistentVisitor;
    
    private SecureVisitor secureVisitor;
    
    private CloudContextVisitor cloudContextVisitor;
    
    private ConfigVisitor configVisitor;
    
    private String entityPackage;

    /**
     * @param persistentVisitor the persistentVisitor to set
     */
    public void setPersistentVisitor(PersistentVisitor persistentVisitor) {
        this.persistentVisitor = persistentVisitor;
    }
    
    /**
     * @param secureVisitor the secureVisitor to set
     */
    public void setSecureVisitor(SecureVisitor secureVisitor) {
        this.secureVisitor = secureVisitor;
    }
    
    /**
     * 
     * @param cloudContextVisitor
     */
    public void setCloudContextVisitor(CloudContextVisitor cloudContextVisitor) {
        this.cloudContextVisitor = cloudContextVisitor;
    }
    
    /**
     * 
     * @param entityPackage
     */
    public void setEntityPackage(String entityPackage) {
        this.entityPackage = entityPackage;
    }
    
    /**
     * Get persist namespace
     * 
     * @return the persistentVisitor
     */
    public PersistentVisitor getPersist() {
        return persistentVisitor;
    }
    
    /**
     * Get secure namespace
     * 
     * @return the secureVisitor
     */
    public SecureVisitor getSecure() {
        return this.secureVisitor;
    }
    
    /**
     * 
     * @return
     */
    public CloudContextVisitor getCloudContext() {
        return cloudContextVisitor;
    }
    
    /**
     * @param configVisitor the configVisitor to set
     */
    public void setConfigVisitor(ConfigVisitor configVisitor) {
        this.configVisitor = configVisitor;
    }
    
    /**
     * @return
     */
    public ConfigVisitor getConfig() {
        return configVisitor;
    }
    
    /**
     * Get all the enums
     * 
     * @return
     */
    @SuppressWarnings("rawtypes")
    @Cacheable("enums")
    public Map<String, Enum[]> getEnums() {
        return EnumUtils.scan(entityPackage);
    }

    /**
     * 枚举必须有 静态 codeOf方法
     * @param enumClassName
     * @param code
     * @return
     * @throws Exception
     */
    public Enum getEnumByCode(String enumClassName, String code)throws Exception{

        Class<?> forName = Class.forName(enumClassName);

        Method method = forName.getMethod("codeOf", String.class);

        if (method == null){
            throw new BusinessException("", "");
        }
        Object invoke = method.invoke(null, code);

        return (Enum) invoke;
    }
}

